/**
 * Copyright (C) 2008 aileron.cc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cc.aileron.web.sso;

import static cc.aileron.commons.global.GlobalSetting.globalSetting;
import static cc.aileron.commons.global.GlobalSetting.globalSettingProperties;
import static cc.aileron.commons.utility.ResourceUtils.resource;

import java.io.IOException;

import cc.aileron.commons.exception.ModuleConfigurationException;
import cc.aileron.commons.resource.ResourceNotFoundException;
import cc.aileron.dao.db.DbDaoModule;
import cc.aileron.template.module.XmlTemplateModule;
import cc.aileron.web.sso.dao.DaoConduct;
import cc.aileron.web.sso.logic.SessionLogic;
import cc.aileron.web.sso.logic.SessionLogicImpl;

import com.google.inject.Binder;
import com.google.inject.Module;
import com.google.inject.Scopes;

/**
 * application-module
 * @author Aileron
 *
 */
public class ApplicationModule implements Module
{
    /**
     * GLOBAL_SETTING
     */
    private static final String GLOBAL_SETTING_FILE_NAME = "/opt/setting/aileron-framework-web-sso.properties";
    
    /**
     * construct
     * @throws ModuleConfigurationException 
     * @throws ResourceNotFoundException 
     * @throws IOException 
     */
    public ApplicationModule() 
    throws ModuleConfigurationException, IOException, ResourceNotFoundException
    {
        globalSettingProperties(resource(GLOBAL_SETTING_FILE_NAME).toProperties());
        this.db = new DbDaoModule(new DaoConduct(globalSettingProperties()));
        this.templateModule = new XmlTemplateModule();
        this.context = getContext();
    }
    
    /**
     * アプリケーションコンテキストの取得
     * @return
     */
    private ApplicationContext getContext()
    {
    	final boolean isSelf = globalSetting("sso.server.self").equals("true");
    	final String singleSignOnServer = globalSetting("sso.server.host");
    	return new ApplicationContext()
        {
			@Override
			public String singleSignOnServer() 
			{
				return singleSignOnServer;
			}

			@Override
			public boolean isSelf() 
			{
				return isSelf;
			}
        };
    }
    
    /**
     * db-module
     */
    private final DbDaoModule db ;
    
    /**
     * template-module
     */
    private final XmlTemplateModule templateModule;
    
    /**
     * application-context
     */
    private final ApplicationContext context;
    
    /*
     * (非 Javadoc)
     * @see com.google.inject.Module#configure(com.google.inject.Binder)
     */
    public void configure(final Binder binder)
    {
        binder.install(db);
        binder.install(templateModule);
        binder.bind(SessionLogic.class).to(SessionLogicImpl.class).in(Scopes.SINGLETON);
        binder.bind(ApplicationContext.class).toInstance(context);
    }
}